/**
* 路由注册
* Author: dong xing
* Date: 2019-01-28
* Time: 15:27
* Email: dong.xing@outlook.com
*/

import Vue from 'vue'
import VueRouter from 'vue-router'
import constantRouterMap from './router'

Vue.use(VueRouter)

const router = new VueRouter({
  mode: 'history',
  base: process.env.BABEL_ENV,
  routes: constantRouterMap,
  scrollBehavior (to, from, savedPosition) {
    if (to.hash) {
      return { selector: to.hash }
    } else if (savedPosition) {
      return savedPosition
    } else {
      return { x: 0, y: 0 }
    }
  }
})

router.beforeEach((to, _, next) => {
  if (to.matched.some(record => record.meta.requiresAuth)) {
    //  todo judge login state
    let auth = true
    if (auth) {
      next({
        path: '/login',
        query: to.fullPath
      })
    } else {
      next()
    }
  } else {
    next()
  }
})

export default router
